package com.eagle.concurrency.example.atomic;

import com.eagle.concurrency.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

@Slf4j
@ThreadSafe
public class AtomicIntegerFieldUpdaterDemo {

	@Getter
	private volatile int count = 100;

	//更新对象中的指定值
	private static AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterDemo> updater =
			AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterDemo.class, "count");


	public static void main(String[] args) {
		AtomicIntegerFieldUpdaterDemo demo = new AtomicIntegerFieldUpdaterDemo();
		boolean b1 = updater.compareAndSet(demo, 100, 120);
		log.info("result="+b1+" count="+demo.getCount());
		boolean b2 = updater.compareAndSet(demo, 120, 130);
		log.info("result="+b2+" count="+demo.getCount());
		boolean b3 = updater.compareAndSet(demo, 120, 150);
		log.info("result="+b3+" count="+demo.getCount());
	}
}